<template>
    <!-- 头部 -->
    <el-header>
        <div class="left">
            <img src="../assets/imgs/shop.png" alt="">
            <span>电商后台管理系统</span>
        </div>
        <div class="right">
            <span>欢迎你:{{ user.username }}</span>&nbsp;
            <el-button type="danger" size="small" @click="logout">退出</el-button>
        </div>
    </el-header>
    <!-- 内容区域 -->
    <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px'">
            <!-- 切换菜单的折叠和显示 -->
            <div class="toggle-button" @click="isCollapse = !isCollapse">|||</div>
            <!-- 侧边栏菜单 -->
            <el-menu :default-active="$route.path" router unique-openedclass="el-menu-vertical-demo" unique-opened
                :collapse="isCollapse">
                <el-sub-menu :index="item.id + ''" v-for="item in menuList" :key="item.id">
                    <template #title>
                        <i :class="iconObj[item.id]"></i>
                        <span>{{ item.authName }}</span>
                    </template>
                    <el-menu-item v-for="subItem in item.children" :key="subItem.id"
                        :index="'/' + item.path + '/' + subItem.path">
                        <i class="el-icon-menu"></i>
                        {{ subItem.authName }}
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <el-main>
            <router-view></router-view>
        </el-main>
    </el-container>
</template>

<script  setup >
import { onMounted, ref, } from 'vue';
import { getMenus, } from '@/apis/user'
import { useUserStore } from '../store/user';
import { useRouter } from 'vue-router';
const user = useUserStore()
const router = useRouter()
const isCollapse = ref(false)
//图标
const iconObj = {
    201: "iconfont icon-shouye",
    125: "iconfont icon-users",
    103: "iconfont icon-tijikongjian",
    101: "iconfont icon-shangpin",
    102: "iconfont icon-danju",
}
// const userInfo = ref(null),
const menuList = ref([])
const getMenuList = () => {
    getMenus().then(res => {
        menuList.value = res.data
    })
}
onMounted(() => {
    getMenuList()
})
//退出
const logout = () => {
    ElMessageBox.confirm(
        '确定退出吗?',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            user.logout()
            router.push("/login")
        })
        .catch(() => {

        })
}
</script>

<style scoped lang="scss">
.el-container {

    // height: 100%;
    &::-webkit-scrollbar {
        display: none
    }
}

.el-header {
    height: 50px !important;
    background: url("../assets/imgs/header_bg.gif") repeat-x center;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: white;

    .left {
        display: flex;
        align-items: center;

        img {
            width: 40px;
            margin-right: 20px;
        }

        span {
            font-size: 20px;
        }
    }

    .right {
        span {
            mask-repeat: 10px;
        }
    }
}

.el-aside {
    float: left;
    background-color: #fff;
    height: calc(100vh - 50px);
    border-right: 1px solid rgba(0, 0, 0, 0.1);
    transition: all 0.8s;

    &::-webkit-scrollbar {
        display: none;
    }

    .el-menu {
        border-right: 0;

        .iconfont {
            margin-right: 10px
        }
    }

    .toggle-button {
        background: #60779d;
        font-size: 10px;
        line-height: 24px;
        text-align: center;
        color: white;
        letter-spacing: 0.2rem;
        cursor: pointer;
    }
}

.el-main {
    background-color: #fff;
    height: calc(100vh - 80px);
    overflow-y: auto;

}
</style>
